﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Controllers
{
    public class UserInfoController : BaseController
    {
        #region 字段
        public readonly GraduationProject.RDBMS.User _userService = new RDBMS.User();
        #endregion

        // GET: UserInfo
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult UpDatePW()
        {
            return View();
        }
        /// <summary>
        /// 登陆页面
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginPage()
        {
            Session.Abandon();//清除全部Session
            return View();
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(User model)
        {
            //先判断用户账号是否存在
            var accounts = new string[] { model.UserAccount };
            User[] result;
            _userService.TrySelectUserByAccount(out result,false, accounts);
            if (result!=null)
            {
                if (result.Length>0)
                {
                    if (!result[0].IsDelete)
                    {
                        //判断密码是否一致
                        if (model.UserPassWd == result[0].UserPassWd)
                        {
                            //设置session
                            Session["loginUser"] = result[0];
                            Session.Timeout = 20;
                            return Json(new { code = 0, msg = "登陆成功" });
                        }
                    }
                  
                }
            }
            return Json(new { code = 1, msg = "登陆失败，账号用户密码错误！" });
        }
        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult RegistrationPage()
        {
            return View();
        }
        /// <summary>
        /// 根据ID查询用户数据
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SelectUserByID(int UserID)
        {
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<User>() });
        }
        /// <summary>
        /// 前台用户注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Insert(User model)
        {
            //先判断用户账号是否存在
            var accounts = new string[] { model.UserAccount };
            bool result;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryGetIsAccountExist(out result, accounts))
                {
                    if (result)
                    {
                        return Json(new { code = 1, msg = "注册失败，用户已存在！" });
                    }
                }
                else
                {
                    return Json(new { code = 1, msg = "注册失败，请稍后重试！" });

                }
        
            }
            model.IsAdmin = 0;
            //插入
            var models = new User[] { model };
            if (model.HeadPortrait==null)
            {
                //插入默认图片
                model.HeadPortrait = "/Content/layui/images/8.jpg";
            }
            bool userResult;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryInsertUser(out userResult, models))
                {
                    return Json(new { code = 0, msg = "注册成功" });
                }
            }
            return Json(new { code = 1, msg = "注册失败，请稍后重试！" });
        }
        /// <summary>
        /// 图片上传先上传服务器（插入成功）返回正确地址后再插入用户信息
        /// </summary>
        /// <param name="NewsImages"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadImage(HttpPostedFileBase file)
        {
            string path;
            if (file != null)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/UploadImg"), Path.GetFileName(file.FileName));
                file.SaveAs(filePath);
                path = "../UploadImg/" + Path.GetFileName(file.FileName);
                return Json(new { code = 0, msg = "成功", url = path });
            }
            else
            {
                path = "../UploadImg";
                return Json(new { code = 1, msg = "抱歉,上传失败", url = path });
            }
        }
      
        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdatePW(string NewPW, string OldPW, string SecPW)
        {
            if (NewPW != SecPW)
            {
                return Json(new { code = 1, msg = "修改失败，两次密码输入不一致！" });
            }
            bool result;
            using (var conn = new RDBMS.User())
            {
                if (conn.TryUpdateUserPassWd(out result, CurrentUser.UserID, NewPW, OldPW))
                {
                    return Json(new { code = 0, msg = "用户密码修改成功" });
                }
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }
    }
}